package com.rc.saas.tenant.controller.pay;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.pay.TenantPayment;
import com.rc.saas.tenant.model.pay.TenantPaymentActivity;
import com.rc.saas.tenant.model.pay.TenantPaymentActivityExample;
import com.rc.saas.tenant.model.pay.TenantPaymentExample;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.service.pay.TenantPaymentActivityService;
import com.rc.saas.tenant.service.pay.TenantPaymentService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author frog
 * @date 2021/9/10
 */
@Controller
@RequestMapping("/tenantPaymentActivity")
public class TenantPaymentActivityController {
    private static Logger logger = LogManager.getLogger(TenantPaymentActivityController.class);

    @Autowired
    private TenantPaymentActivityService tenantPaymentActivityService;

    @Autowired
    private TenantPaymentService tenantPaymentService;

    /**
     * 跳转支付渠道优惠活动信息页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/templates/pay/tenantPaymentActivity";
    }

    /**
     * 返回支付活动信息列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantPaymentActivity", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantPaymentActivity(BaseSearch baseSearch, String remark) {
        PageInfo<TenantPaymentActivity> pageInfo = tenantPaymentActivityService.listPageTenantPaymentActivity(baseSearch, remark);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加支付活动信息页面
     *
     * @return
     */
    @RequestMapping(value = "/addTenantPaymentActivity", method = RequestMethod.GET)
    public String addTenantPaymentActivity() {
        return "/templates/pay/addTenantPaymentActivity";
    }

    /**
     * 跳转编辑支付活动信息页面
     *
     * @return
     */
    @RequestMapping(value = "/editTenantPaymentActivity/{paymentActivityId}", method = RequestMethod.GET)
    public String editTenantPaymentActivity(@PathVariable("paymentActivityId") Long paymentActivityId, Model model) {

        TenantPaymentActivity tenantPaymentActivity = tenantPaymentActivityService.selectByPrimaryKey(paymentActivityId);
        TenantPayment tenantPayment = tenantPaymentService.selectByPrimaryKey(tenantPaymentActivity.getTenantPaymentId());
        tenantPaymentActivity.setPayType(tenantPayment.getPaymentType());

        model.addAttribute("tenantPaymentActivity", tenantPaymentActivity);
        return "/templates/pay/editTenantPaymentActivity";
    }

    /**
     * 保存/编辑支付活动信息
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantPaymentActivity", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantPaymentActivity(TenantPaymentActivity tenantPaymentActivity) {
        if (!BeanUtil.isValid(tenantPaymentActivity.getStartTime(), tenantPaymentActivity.getEndTime(), tenantPaymentActivity.getRemark())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        TenantUser user = ShiroUtils.getSessionUser();
        try {
            TenantPaymentExample tenantPaymentExample = new TenantPaymentExample();
            tenantPaymentExample.createCriteria().andPaymentTypeEqualTo(tenantPaymentActivity.getPayType())
                    .andTenantCodeEqualTo(user.getTenantCode());
            List<TenantPayment> tenantPayments = tenantPaymentService.selectByExample(tenantPaymentExample);
            if (CollectionUtils.isEmpty(tenantPayments)) {
                return TResponse.FAIL("请先设置此种支付方式");
            }

            tenantPaymentActivity.setTenantPaymentId(tenantPayments.get(0).getTenantPaymentId());

            TenantPaymentActivityExample tenantPaymentActivityExample = new TenantPaymentActivityExample();
            tenantPaymentActivityExample.createCriteria().andChannelTypeEqualTo(tenantPaymentActivity.getChannelType())
                    .andTenantPaymentIdEqualTo(tenantPaymentActivity.getTenantPaymentId()).andStatusEqualTo("0");
            List<TenantPaymentActivity> tenantPaymentActivities = tenantPaymentActivityService.selectByExample(tenantPaymentActivityExample);

            if (!CollectionUtils.isEmpty(tenantPaymentActivities)) {
                if (tenantPaymentActivity.getPaymentActivityId() != null) {
                    if (!tenantPaymentActivities.get(0).getPaymentActivityId().equals(tenantPaymentActivity.getPaymentActivityId())) {
                        return TResponse.FAIL("已存在相同终端以及同支付方式的记录,无法编辑");
                    }

                } else {
                    return TResponse.FAIL("已存在相同终端以及同支付方式的记录,无法新增");
                }

            }

            if (tenantPaymentActivity.getPaymentActivityId() != null) {

                tenantPaymentActivityService.updateByPrimaryKeySelective(tenantPaymentActivity);
            } else {
                tenantPaymentActivityService.insertSelective(tenantPaymentActivity);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存支付活动信息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 删除支付活动信息
     *
     * @return
     */
    @RequestMapping(value = "/deleteTenantPaymentActivity", method = RequestMethod.POST)
    @ResponseBody
    public TResponse deleteBanner(Long paymentActivityId) {
        try {
            tenantPaymentActivityService.deleteByPrimaryKey(paymentActivityId);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("删除支付活动信息失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 更改状态
     *
     * @return
     */
    @RequestMapping(value = "/updateTenantPaymentActivityStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateTenantPaymentActivityStatus(Long paymentActivityId, String status) {
        try {
            TenantPaymentActivity tenantPaymentActivity = tenantPaymentActivityService.selectByPrimaryKey(paymentActivityId);
            if ("0".equals(status)) {
                TenantPaymentActivityExample tenantPaymentActivityExample = new TenantPaymentActivityExample();
                tenantPaymentActivityExample.createCriteria().andChannelTypeEqualTo(tenantPaymentActivity.getChannelType())
                        .andTenantPaymentIdEqualTo(tenantPaymentActivity.getTenantPaymentId()).andStatusEqualTo("0");
                List<TenantPaymentActivity> tenantPaymentActivities = tenantPaymentActivityService.selectByExample(tenantPaymentActivityExample);

                if (!CollectionUtils.isEmpty(tenantPaymentActivities)) {
                    return TResponse.FAIL("已存在相同终端以及同支付方式的记录,无法新增");
                }
            }

            tenantPaymentActivity.setStatus(status);
            tenantPaymentActivityService.updateByPrimaryKeySelective(tenantPaymentActivity);
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("更改状态失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

}
